﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;
using System.Data;

namespace WebSite.admin.member
{
    public partial class member : BaseAdmin
    {

        readonly MemberAccount theMemberAccount = new MemberAccount();
        // Methods
        private void Bind()
        {
            string sql = "select * from DB_Memberank";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];

            if ((base.Request.QueryString["mid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("update DB_Member set flag=2 where memberid=" + base.Request.QueryString["mid"].ToString() + "");
                }
                base.Response.Write("<script language='javascript'>window.location.href='member.aspx';</script>");
                base.Response.End();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind("");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        DataSet ds;
                        System.Text.StringBuilder sbContent = new System.Text.StringBuilder();
                        ds = theMemberAccount.GetListByMemberId(int.Parse(e.Row.Cells[5].Text.ToString()));
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            int iCount = 0;
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                              
                                sbContent.Append(dr["cardcode"].ToString() + "|");
                                iCount++;
                                if (iCount % 5 == 0)
                                {
                                    sbContent.Append("\n");
                                }
                            }
                        }
                        e.Row.Cells[7].Text = sbContent.ToString();
                        e.Row.Cells[1].Width = 180;
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                    e.Row.Cells[5].Visible = false;
                }
            }
        }

 

        private void GridViewBind(string Sqlsort)
        {
            string sql = "select * from DB_Member where 1=1 and isnull(flag,0)!=2 ";
          
            if (this.memberlogin.Value.Trim() != "")
            {
                sql = sql + " and memberlogin like '%" + this.memberlogin.Value.Trim() + "%' ";
            }
            if (this.telPhone.Value.Trim() != "")
            {
                sql = sql + " and tel1 like '%" + this.telPhone.Value.Trim() + "%'";
            }
            if (this.cardCode.Value.Trim() != "")
            {
                sql = sql + " and memberid in (select memberid from tbMemberAccount MA join DB_Card C on MA.Cardid = C.CardID and C.CardCode like '%" + this.cardCode.Value.Trim() + "%')";
            }

            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    SQLAccess.DoSql("update DB_Member set flag=2  where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_Memberadd where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_account where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_msg where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_car where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_order where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_accountuser where memberid=" + this.GridView1.DataKeys[i].Value + "");
                    //SQLAccess.DoSql("DELETE FROM DB_Score where memberid=" + this.GridView1.DataKeys[i].Value + "");
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='member.aspx';</script>");
            base.Response.End();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
   
            this.ImageButton2.Attributes["onclick"] = "return CheckSelect(true,'del');";
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.UserList);
                this.GridViewBind("");
                this.Bind();
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind("");
        }

    }
}